package com.yanqu.road.dao.impl.activity.douluodalu;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.douluodalu.data.DouLuoDaLuNpcBloodData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DouLuoDaLuNpcBloodDataDaoImpl extends TempDataObjectDao<DouLuoDaLuNpcBloodData> {

    public static String getDouLuoDaLuNpcBloodDataSql = "select * from `t_c_douluodalu_npc_blood` where `activityId`=? and groupId=?";
    public Map<Integer, DouLuoDaLuNpcBloodData> getDouLuoDaLuNpcBloodData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, DouLuoDaLuNpcBloodData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getDouLuoDaLuNpcBloodDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                DouLuoDaLuNpcBloodData data = parse(rs);
                map.put(data.getEventId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertDouLuoDaLuNpcBloodDataSql = "insert into `t_c_douluodalu_npc_blood`(activityId,eventId,blood,updateTime," +
            "`userNum`, `baseValue1`, `startRank1`, `endRank1`, `earnSum1`, `num1`, `baseValue2`, `startRank2`, `endRank2`, `num2`, `earnSum2`, `baseValue3`, `baseValue4`,groupId) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DouLuoDaLuNpcBloodData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUserNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getBaseValue1()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStartRank1()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEndRank1()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEarnSum1()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNum1()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getBaseValue2()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStartRank2()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEndRank2()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNum2()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEarnSum2()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getBaseValue3()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getBaseValue4()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(insertDouLuoDaLuNpcBloodDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDouLuoDaLuNpcBloodDataSql = "update `t_c_douluodalu_npc_blood` set blood=?,updateTime=?," +
            "`userNum`=?, `baseValue1`=?, `startRank1`=?, `endRank1`=?, `earnSum1`=?, `num1`=?, `baseValue2`=?, `startRank2`=?, `endRank2`=?, `num2`=?, `earnSum2`=?, `baseValue3`=?, `baseValue4`=? where activityId=? and eventId=? and groupId=?";

    @Override
    protected boolean updateData(DouLuoDaLuNpcBloodData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUserNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getBaseValue1()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStartRank1()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEndRank1()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEarnSum1()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNum1()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getBaseValue2()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStartRank2()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEndRank2()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNum2()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEarnSum2()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getBaseValue3()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getBaseValue4()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(updateDouLuoDaLuNpcBloodDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DouLuoDaLuNpcBloodData parseData(ResultSet rs) throws SQLException {
        DouLuoDaLuNpcBloodData data = new DouLuoDaLuNpcBloodData();
        data.setActivityId(rs.getInt("activityId"));
        data.setEventId(rs.getInt("eventId"));
        data.setBlood(rs.getLong("blood"));
        data.setUpdateTime(rs.getLong("updateTime"));
        data.setUserNum(rs.getInt("userNum"));
        data.setBaseValue1(rs.getLong("baseValue1"));
        data.setStartRank1(rs.getInt("startRank1"));
        data.setEndRank1(rs.getInt("endRank1"));
        data.setNum1(rs.getInt("num1"));
        data.setEarnSum1(rs.getLong("earnSum1"));
        data.setBaseValue2(rs.getLong("baseValue2"));
        data.setStartRank2(rs.getInt("startRank2"));
        data.setEndRank2(rs.getInt("endRank2"));
        data.setNum2(rs.getInt("num2"));
        data.setEarnSum2(rs.getLong("earnSum2"));
        data.setBaseValue3(rs.getLong("baseValue3"));
        data.setBaseValue4(rs.getLong("baseValue4"));
        data.setGroupId(rs.getInt("groupId"));
        return data;
    }
}
